package com.ybxx.asset.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.ybxx.asset.model.Building;
import com.ybxx.asset.model.Company;
import com.ybxx.asset.model.HousingBuilding;
import com.ybxx.asset.model.vo.BuildingVO;
import com.ybxx.asset.model.vo.HousingBuildingVO;
import com.ybxx.asset.service.BuildingService;
import com.ybxx.asset.service.CompanyService;
import com.ybxx.asset.service.HousingBuildingService;
import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.DateUtil;
import com.ybxx.core.util.ExportExcel;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;

public class HousingBuildingAction extends BaseAction {
	@Resource
	private CompanyService companyService;
	@Resource
	private HousingBuildingService housingBuildingService;
	private List<Company> companys;
	private List<HousingBuilding> hbs;
	private List<HousingBuildingVO> vos;
	private HousingBuilding hb;
	private String msg="succ";
	/*打印*/
	public String print(){
		hbs=housingBuildingService.exportExcel_Print(hb);
		vos=new ArrayList<HousingBuildingVO>();
		if(hbs!=null){
			int i=1;
			for(HousingBuilding hb:hbs){
				HousingBuildingVO vo=new HousingBuildingVO();
				vo.setSeq(i+"");
				vo.setBancode(hb.getBancode());
				vo.setBuildaddr(hb.getBuildaddr());
				vo.setBuildarea(hb.getBuildarea());
				vo.setBuildmonth(hb.getBuildmonth());
				vo.setCname(hb.getCompany().getCname());
				vo.setContent(hb.getContent());
				vo.setFwzl(hb.getFwzl());
				vo.setLayer(hb.getLayer());
				vo.setMdept(hb.getMdept());
				vo.setPrice(hb.getPrice());
				vo.setResetprice(hb.getResetprice());
				vo.setRoomnum(hb.getRoomnum());
				vo.setStructlayer(hb.getStructlayer());
				vos.add(vo);
				i++;
			}
		}
		return "print";
	}
	/*导出*/
	public String exportExcel(){
		hbs=housingBuildingService.exportExcel_Print(hb);
		vos=new ArrayList<HousingBuildingVO>();
		if(hbs!=null){
			int i=1;
			for(HousingBuilding hb:hbs){
				HousingBuildingVO vo=new HousingBuildingVO();
				vo.setSeq(i+"");
				vo.setBancode(hb.getBancode());
				vo.setBuildaddr(hb.getBuildaddr());
				vo.setBuildarea(hb.getBuildarea());
				vo.setBuildmonth(hb.getBuildmonth());
				vo.setCname(hb.getCompany().getCname());
				vo.setContent(hb.getContent());
				vo.setFwzl(hb.getFwzl());
				vo.setLayer(hb.getLayer());
				vo.setMdept(hb.getMdept());
				vo.setPrice(hb.getPrice());
				vo.setResetprice(hb.getResetprice());
				vo.setRoomnum(hb.getRoomnum());
				vo.setStructlayer(hb.getStructlayer());
				vos.add(vo);
				i++;
			}
		}
		ExportExcel<HousingBuildingVO> ex=new ExportExcel<HousingBuildingVO>();
		OutputStream out;
		try {
			out = super.getResponse().getOutputStream();
			super.getResponse().reset();// 清空输出流
			super.getResponse().setContentType("text/html; charset=UTF-8");
			super.getResponse().setHeader("Content-disposition",
					"attachment; filename="+DateUtil.formatDateYYYYMMdd(new Date())+".xls");// 设定输出文件头
			super.getResponse().setContentType("application/msexcel");// 定义输出类型
			String[] headers={"序号","企业名称","房屋座落","楼栋号","建筑面积","套(间)数","层数","结构等级","建筑占地","竣工年代","造价","重置价","管理（使用）部门","备注"};
	        ex.exportExcel("住宅楼栋信息",headers, vos, out);
	        out.flush();
	        out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/*删除*/
	public String remove(){
		try{
			housingBuildingService.remove(hb.getHbid());
		}catch(Exception ex){
			msg="房屋建筑物删除失败";
		}finally{
			return "succ";
		}
	}
	/*加载查看时候需要展示的数据*/
	public String view(){
		if(hb!=null && StringUtils.isNotEmpty(hb.getHbid())){
			hb=housingBuildingService.get(hb.getHbid());
		}
		return "view";
	}
	public String init(){
		companys=companyService.getAll();
		return "init";
	}
	public String queryPage(){
		QueryFilter filter=new QueryFilter(getRequest());
		hbs=housingBuildingService.queryPage(hb,filter);
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "queryPage";
	}
	/*保存新增，修改信息*/
	public String save(){
		if(StringUtils.isEmpty(hb.getHbid())){
			hb.setHbid(UUIDGenerator.getUUID());
		}
		try{
			housingBuildingService.save(hb);
		}catch(Exception ex){
			msg="住宅楼栋信息保存失败.";
		}finally{
			return "succ";
		}
	}
	/*加载修改时候需要展示的数据*/
	public String load(){
		companys=companyService.getAll();
		if(hb!=null && StringUtils.isNotEmpty(hb.getHbid())){
			hb=housingBuildingService.get(hb.getHbid());
		}
		return "load";
	}
	public List<Company> getCompanys() {
		return companys;
	}
	public void setCompanys(List<Company> companys) {
		this.companys = companys;
	}
	public List<HousingBuilding> getHbs() {
		return hbs;
	}
	public void setHbs(List<HousingBuilding> hbs) {
		this.hbs = hbs;
	}
	public HousingBuilding getHb() {
		return hb;
	}
	public void setHb(HousingBuilding hb) {
		this.hb = hb;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public List<HousingBuildingVO> getVos() {
		return vos;
	}
	public void setVos(List<HousingBuildingVO> vos) {
		this.vos = vos;
	}
}
